﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace JobShop
{
    [Description("Represents the standard job shop problem")]
    public class TcProblem
    {
        public Int32 M { get; private set; }
        public Int32 N { get; private set; }
        public String Name { get; private set; }
        public Int32 Row { get; private set; }
        public Int32 Column { get; private set; }
        public Int32[,] Machines { get; private set; }
        public Int32[,] Times { get; private set; }
        public Int32[,] TaskOrder { get; private set; }
        public Boolean IsValid { get; private set; }

        public TcProblem(String prmName, Int32 prmJob, Int32 prmMachine)
        {
            Name = prmName;
            N = prmJob;
            M = prmMachine;
            Row = prmJob;
            Column = prmMachine;
            Machines = new Int32[Row, Column];
            Times = new Int32[Row, Column];
            TaskOrder = new Int32[Row, Column];
        }

        public Boolean TryParse(String prmData)
        {
            try
            {
                String[] lines = prmData.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (lines.Length != Row)
                    return IsValid = false;

                for (int i = 0; i < N; i++)
                {
                    String[] tokens = lines[i].Split(" \t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length != Column * 2)
                        return IsValid = false;

                    for (int j = 0; j < M; j++)
                    {
                        if (!Int32.TryParse(tokens[j * 2], out Machines[i, j]))
                            return IsValid = false;

                        TaskOrder[i, Machines[i, j]] = j;

                        if (!Int32.TryParse(tokens[j * 2 + 1], out Times[i, j]))
                            return IsValid = false;

                    }
                }
                return IsValid = true;
            }
            catch (Exception)
            {
                return IsValid = false;
            }
        }
    }
}